package com.zl.task.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zl.task.dao.ServiceEnterprisedMapper;
import com.zl.task.dao.ServiceIndustryMapper;
import com.zl.task.dao.ServiceMechanismMapper;
import com.zl.task.dao.ServiceServerMapper;
import com.zl.task.dto.EnterpriseDTO;
import com.zl.task.pojo.ServiceEnterprised;
import com.zl.task.pojo.ServiceIndustry;
import com.zl.task.pojo.ServiceMechanism;
import com.zl.task.pojo.ServiceServer;
import com.zl.task.service.IServiceChinaService;
import com.zl.task.service.IServiceEnterprisedService;
import com.zl.task.service.IServiceToken;
import com.zl.task.util.CheckMessage;
import com.zl.task.vo.EnterprisedLoginVO;
import com.zl.task.vo.MechanismLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.zl.task.commom.ServiceCode.ENTERPRISE_TYPE;
import static com.zl.task.commom.ServiceCode.MECHANISM_TYPE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yinlingeng
 * @since 2021-07-23
 */
@Service
public class ServiceEnterprisedServiceImpl extends ServiceImpl<ServiceEnterprisedMapper, ServiceEnterprised> implements IServiceEnterprisedService {

    @Autowired
    private IServiceToken serviceToken;
    @Resource
    ServiceIndustryMapper industryMapper;
    @Resource
    ServiceMechanismMapper mechanismMapper;
    @Resource
    ServiceServerMapper serverMapper;

    /**
     * 企业登录
     *
     * @param userName
     * @param password
     * @return
     */
    @Override
    public EnterprisedLoginVO getEnterprised(String userName, String password) throws Exception {
        ServiceEnterprised serviceEnterprised = baseMapper.selectOne(Wrappers.<ServiceEnterprised>lambdaQuery()
                .eq(ServiceEnterprised::getEnterprisePhone, userName)
                .eq(ServiceEnterprised::getEnterprisePassword, password));
        if (serviceEnterprised != null){
            String token = serviceToken.getToken(serviceEnterprised.getEnterprisedId().toString(), serviceEnterprised.getEnterprisePassword());
            EnterprisedLoginVO enterprisedLoginVO = new EnterprisedLoginVO();
            enterprisedLoginVO.setEnterpriseId(serviceEnterprised.getEnterprisedId().toString());
            enterprisedLoginVO.setEnterpriseName(serviceEnterprised.getEnterpriseName());
            enterprisedLoginVO.setEnterpriseContacts(serviceEnterprised.getEnterpriseContacts());
            enterprisedLoginVO.setEnterprisePhone(serviceEnterprised.getEnterprisePhone());
            enterprisedLoginVO.setEnterpriseEmial(serviceEnterprised.getEnterpriseEmial());
            enterprisedLoginVO.setEnterpriseIndustry( industryMapper.selectOne(Wrappers.<ServiceIndustry>lambdaQuery()
                    .eq(ServiceIndustry::getIndustryId, serviceEnterprised.getEnterpriseIndustry())).getIndustryName());
            enterprisedLoginVO.setToken(token);
            return enterprisedLoginVO;
        }
        throw new Exception("登录失败，账号或密码错误");
    }

    /**
     * 用户登录
     *
     * @param userName
     * @param password
     * @param userType
     * @return
     * @throws Exception
     */
    @Override
    public Object userLogin(String userName, String password, String userType) throws Exception {

        if (userType.equals(ENTERPRISE_TYPE)) {
            ServiceEnterprised serviceEnterprised = baseMapper.selectOne(Wrappers.<ServiceEnterprised>lambdaQuery()
                    .eq(ServiceEnterprised::getEnterprisePhone, userName)
                    .eq(ServiceEnterprised::getEnterprisePassword, password));
            if (serviceEnterprised != null){
                String token = serviceToken.getToken(serviceEnterprised.getEnterprisedId().toString(), serviceEnterprised.getEnterprisePassword());
                EnterprisedLoginVO enterprisedLoginVO = new EnterprisedLoginVO();
                enterprisedLoginVO.setEnterpriseId(serviceEnterprised.getEnterprisedId().toString());
                enterprisedLoginVO.setEnterpriseName(serviceEnterprised.getEnterpriseName());
                enterprisedLoginVO.setEnterpriseContacts(serviceEnterprised.getEnterpriseContacts());
                enterprisedLoginVO.setEnterprisePhone(serviceEnterprised.getEnterprisePhone());
                enterprisedLoginVO.setEnterpriseEmial(serviceEnterprised.getEnterpriseEmial());
                enterprisedLoginVO.setEnterpriseIndustry( industryMapper.selectOne(Wrappers.<ServiceIndustry>lambdaQuery()
                        .eq(ServiceIndustry::getIndustryId, serviceEnterprised.getEnterpriseIndustry())).getIndustryName());
                enterprisedLoginVO.setToken(token);
                return enterprisedLoginVO;
            }
        }else if (userType.equals(MECHANISM_TYPE)){
            ServiceMechanism serviceMechanism = mechanismMapper.selectOne(Wrappers.<ServiceMechanism>lambdaQuery()
                    .eq(ServiceMechanism::getMechanismPhone, userName)
                    .eq(ServiceMechanism::getMechanismPassword, password));
            if (serviceMechanism != null) {
                String token = serviceToken.getToken(serviceMechanism.getMechanismId().toString(), serviceMechanism.getMechanismPassword());
                MechanismLoginVO mechanismLoginVO = new MechanismLoginVO();
                mechanismLoginVO.setMechanismId(serviceMechanism.getMechanismId().toString());
                mechanismLoginVO.setMechanismName(serviceMechanism.getMechanismName());
                mechanismLoginVO.setMechanismContacts(serviceMechanism.getMechanismContacts());
                mechanismLoginVO.setMechanismPhone(serviceMechanism.getMechanismPhone());
                mechanismLoginVO.setToken(token);
                String[] split = serviceMechanism.getMechanismType().split(",");
                Set<String> collect = Arrays.stream(split).collect(Collectors.toSet());
                List<ServiceServer> serviceServers = serverMapper.selectList(Wrappers.<ServiceServer>lambdaQuery()
                        .in(ServiceServer::getServerId, collect));
                mechanismLoginVO.setMechanismType(serviceServers);
                return mechanismLoginVO;
            }
        }
        throw new Exception("登录失败，账号或密码错误");
    }

    /**
     * 企业信息维护（修改）
     *
     * @return
     */
    @Override
    public Boolean updateEnterprised(EnterpriseDTO enterpriseDTO) throws Exception {
        ServiceEnterprised serviceEnterprised = new ServiceEnterprised();
        serviceEnterprised.setEnterprisedId(Integer.valueOf(enterpriseDTO.getEnterprisedId()).longValue());
        serviceEnterprised.setEnterpriseName(enterpriseDTO.getEnterpriseName());
        serviceEnterprised.setEnterpriseContacts(enterpriseDTO.getEnterpriseContacts());
        serviceEnterprised.setEnterprisePhone(CheckMessage.checkPhone(enterpriseDTO.getEnterprisePhone()));
        serviceEnterprised.setEnterpriseEmial(enterpriseDTO.getEnterpriseEmial());
        serviceEnterprised.setEnterpriseCode(enterpriseDTO.getEnterpriseCode());
        serviceEnterprised.setEnterpriseIndustry(Integer.valueOf(enterpriseDTO.getEnterpriseIndustry()));
        if (baseMapper.updateById(serviceEnterprised)>0) {
            return true;
        }
        throw new Exception("修改失败");
    }
    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @Override
    public ServiceEnterprised getEnterprisedById(String id) {
        ServiceEnterprised serviceEnterprised = baseMapper.selectOne(Wrappers.<ServiceEnterprised>lambdaQuery()
                .eq(ServiceEnterprised::getEnterprisedId, id));
        return serviceEnterprised;
    }

    /**
     * @return List<ServiceEnterprised>
     */
    @Override
    public List<ServiceEnterprised> listEnterprised() {
        return baseMapper.selectList(Wrappers.<ServiceEnterprised>lambdaQuery());
    }
}
